﻿#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"

#if 1
int main()
{
	int flag = 0;
	Helper* helper = new Helper();
	cvNamedWindow("testWnd",1);
	cvNamedWindow("testWnd1",1);

	CvRect screen = cvRect(0,0,0,0);
	while(1)
	{
		cvShowImage("testWnd", helper->GetOriginImage() );
		int ret = helper->GetSkinColor(H_YCbCr);
	
		printf("label count:%d, Screen(%d/%d)(%d/%d)\n",ret, screen.x, screen.y, screen.width, screen.height);

		if( cvWaitKey(10) >= 0 )
		{
			screen = helper->FindScreenRect();
			flag = 1;
		}
		
		cvShowImage("testWnd1", helper->GetOutputImage() );
	}
	cvDestroyAllWindows();

	return 0;
}


#endif
#if 0

int main()
{
	IplImage* origin = NULL;
	IplImage* converted = NULL;
	IplImage* ch1, *ch2, *ch3;
	int value1 = 131;
	int value2 = 0;

	cvNamedWindow("Original", 0 );
	cvNamedWindow("Converted", 0 );

	//컨투어 라벨링용 메모리 공간 할당
	CvMemStorage* storage = cvCreateMemStorage(0);
	CvSeq* contour = NULL;

	//폰트 설정
	CvFont* font = new CvFont;
	cvInitFont(font, CV_FONT_VECTOR0, 0.5f,0.5f, 0, 2);

	//정지이미지로 처리하는 부분
	//origin = cvLoadImage("C:/Documents and Settings/lohan/My Documents/강의/2008_1/캡스톤/Sources/OpenCAM/OpenCAM/pt.jpg");
	
	//캠 영상 받는 부분
	
	CvCapture* capture = cvCaptureFromCAM(0);
	cvGrabFrame( capture );
	origin = cvRetrieveFrame( capture );	
	

	//윈도우와 트랙바 생성
	cvResizeWindow("Original", cvGetSize(origin).width,cvGetSize(origin).height);
	cvResizeWindow("Converted", cvGetSize(origin).width,cvGetSize(origin).height);

	cvCreateTrackbar("Cr","Converted", &value1, 255, NULL);
	cvCreateTrackbar("Cb","Converted", &value2, 255, NULL);

	//각 채널을 담을 이미지공간 생성
	converted	= cvCreateImage(cvGetSize(origin), 8, 3);
	ch1			= cvCreateImage(cvGetSize(origin), 8, 1);
	ch2			= cvCreateImage(cvGetSize(origin), 8, 1);
	ch3			= cvCreateImage(cvGetSize(origin), 8, 1);
	
	
	//메인 루프
	while(1)
	{
		cvGrabFrame( capture );
		origin = cvRetrieveFrame( capture );	
		cvCvtColor(origin, converted, CV_RGB2YCrCb);

		//Cr만 Thresholding - 일단 라벨링까지 고고싱
		cvSplit(converted, ch1, ch2, ch3, NULL );
		//cvThreshold(ch2, ch2, value1, value2,CV_THRESH_BINARY);
		cvThreshold(ch3, ch3, value1, 255,CV_THRESH_BINARY);
		
		//침식확장으로 노이즈 제거
		cvErode(ch3, ch3, NULL, 2);
		cvDilate(ch3, ch3, NULL, 2);

		cvZero(converted);

		//컨투어 라벨링
		cvFindContours(ch3, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

		int counter = 0;
		for ( ; contour != NULL; contour = contour->h_next)
		{
			counter++;
			CvScalar color = CV_RGB(rand()%255, rand()%255, rand()%255 );
			cvDrawContours(converted, contour, color, color, -1,-1,8);
			CvRect rect = cvBoundingRect(contour, 0);
			char name[5];
			sprintf(name, "%d", counter);

			cvPutText(converted, name, 
					  cvPoint(rect.x+rect.width/2, rect.y+rect.height/2),
					  font, cvScalar(255,255,255));


		}

		
		//이미지 뷰어
		cvShowImage("Original",origin);
		cvShowImage("Converted",converted);

		printf("/nvalue1:%d value2:%d", value1, value2);
//		sleep(10);
		
		if( cvWaitKey(10) >= 0 )
			break;
	}

	cvReleaseImage( &converted);
	cvReleaseImage( &origin);

	cvDestroyWindow( "Original" );
	cvDestroyWindow( "Converted" );

	//캠 닫기
	cvReleaseCapture( &capture );


	return 0;
}

/*
int main()
{
	IplImage* origin = NULL;
	IplImage* converted = NULL;
	

	CvCapture* capture = cvCaptureFromCAM(0);
	cvNamedWindow("Original", 0 );
	cvNamedWindow("Converted", 0 );

	//2 read arch.
	cvGrabFrame( capture );
	origin = cvRetrieveFrame( capture );	
	converted = cvCreateImage( cvGetSize(origin), IPL_DEPTH_8U, 3);

	while(1)
	{		
		cvCvtColor(origin, converted, CV_RGB2YCrCb);

		cvShowImage("Original",origin);
		cvShowImage("Converted",converted);


		if( cvWaitKey(10) >= 0 )
			break;

	}
	cvReleaseImage( &converted);
	cvReleaseImage( &origin);
	cvReleaseCapture( &capture );

	cvDestroyWindow( "Original" );
	cvDestroyWindow( "Converted" );

	return 0;
	
}
*/

#endif